package com.pig4cloud.pig.village.welfare.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.village.api.dto.WelfareRuleDTO;
import com.pig4cloud.pig.village.api.entity.WelfareRule;
import com.pig4cloud.pig.village.api.request.WelfareRuleAdd;
import com.pig4cloud.pig.village.api.request.WelfareRuleSearch;
import com.pig4cloud.pig.village.welfare.service.IWelfareRuleService;
import com.pig4cloud.pig.village.welfare.service.IWelfareService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author limbray.li
 * @date 2021/6/19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/welfareRule")
@Api(value = "welfareRule", tags = "福利规则")
public class WelfareRuleController {
	@Resource
	private IWelfareRuleService welfareRuleService;

	@ApiOperation(value = "查询")
	@GetMapping("/page")
	public R<IPage<WelfareRuleDTO>> page(Integer pageNum, Integer pageSize, WelfareRuleSearch search) {
		return R.ok(welfareRuleService.pageBySearch(new Page(pageNum, pageSize), search));
	}

	@ApiOperation(value = "删除")
	@PutMapping("/delete")
	public R delete(@RequestBody WelfareRule rule) {
		return R.ok(welfareRuleService.removeById(rule.getId()));
	}

	@ApiOperation(value = "删除")
	@PutMapping("/deleteById")
	public R delete(Long id) {
		return R.ok(welfareRuleService.removeById(id));
	}

	@ApiOperation(value = "查找byId")
	@GetMapping("/get")
	public R get(Long id) {
		return R.ok(welfareRuleService.getById(id));
	}

	@ApiOperation(value = "增加")
	@PostMapping("/add")
	public R add(@RequestBody WelfareRuleAdd rule) {
		//此处的rule由子类变父类，请注意
		welfareRuleService.save(rule);
		welfareRuleService.updateRulesById(rule.getId());
		return R.ok(rule.getId());
	}

	@ApiOperation(value = "修改")
	@PutMapping("/update")
	public R update(@RequestBody WelfareRuleAdd rule) {
		welfareRuleService.updateById(rule);
		welfareRuleService.updateRulesById(rule.getId());
		return R.ok(rule.getId());
	}

	@Deprecated
	@ApiOperation(value = "更新规则")
	@PutMapping("/updateRules")
	public R updateRules(@ApiParam(name = "rules", value = "规则代码") @RequestParam String rules,
						 @ApiParam(name = "id", value = "规则id") @RequestParam Long id,
						 @ApiParam(name = "welfareId", value = "福利id") @RequestParam Long welfareId) {
		return R.ok(welfareRuleService.updateRulesById(rules, id, welfareId));
	}
}